package my.study.java.lang.Number;

public class IntOverFlowEx01 {
	private static final long MAX_INT = 4294967295L;;

	/**
	 * 
	 * Int达到最大值后 溢出的例子
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Integer.MAX_VALUE: " + Integer.MAX_VALUE);
		System.out.println(formatNumber(Integer.toBinaryString(Integer.MAX_VALUE)));
		long maxInt = Integer.MAX_VALUE + 1;
		System.out.println("Integer.MAX_VALUE + 1: " + maxInt);
		System.out.println(formatNumber(Long.toBinaryString(maxInt)));
		
		int overInt = Integer.MAX_VALUE + 1;
		System.out.println("Integer.MAX_VALUE + 1: " + overInt);
		System.out.println(formatNumber(Integer.toBinaryString(overInt)));
		
		long maxInt2 = int2Long(maxInt);
		System.out.println("Integer.MAX_VALUE + 1: " + maxInt2);
		System.out.println(formatNumber(Long.toBinaryString(maxInt2)));

	}
	
	public static String formatNumber(String arg) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < arg.length(); i++) {
			if (i != 0 && i % 4 == 0) {
				sb.append("\t");
			}
			sb.append(arg.charAt(i));
		}
		return sb.toString();
	}
	/**
	 * 修复溢出情况
	 * @param l
	 * @return
	 */
	public static long int2Long(long l) {
    	return  l & MAX_INT;
    }

}
